package com.v2future.sjt.collegecheckingattendance.event;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.v2future.sjt.collegecheckingattendance.LazyFragment;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.event.adapter.EventListAdapter;
import com.v2future.sjt.collegecheckingattendance.event.adapter.EventTypeAdapter;
import com.v2future.sjt.collegecheckingattendance.event.bean.EventBean;
import com.v2future.sjt.collegecheckingattendance.event.bean.OneEventBean;
import com.v2future.sjt.collegecheckingattendance.event.bean.OneEventTypeBean;
import com.v2future.sjt.collegecheckingattendance.event.bean.SliderBean;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DensityUtil;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.ListViewInScroll;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 活动主页
 * @author shijianting
 * @time 2016/4/14 16:25
 */
public class EventFragment extends LazyFragment {

    private ListView eventTypeLv;

    private EventTypeAdapter eventTypeAdapter;

    private List<OneEventTypeBean> eventTypeList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_event, container, false);
        initView(view);
        return view;
    }

    public void onNewIntent(Intent intent){

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initView(View view) {
        eventTypeLv = (ListView) view.findViewById(R.id.lv_event_type);
        getBaseActivity().showTitleLoading();
        doGetEventType();
    }

    private void refreshView(){
        eventTypeAdapter = new EventTypeAdapter(eventTypeList, getBaseActivity());
        eventTypeLv.setAdapter(eventTypeAdapter);
        eventTypeLv.setOnItemClickListener(eventTypeListener);
        if (eventTypeList.size() == 0) {
            getBaseActivity().tip("暂无信息类型");
        }
    }


    @Override
    protected void lazyLoad() {

    }

    /**
     * 获取活动类别
     */
    private void doGetEventType(){
        BaseRequestParams params = new BaseRequestParams(getBaseActivity(), getBaseActivity().formatUrl(Constants.EVENT_TYPE));
        NetUtils.doGetRequest(getBaseActivity(), params, new NetTaskListener<EventTypeResponseBean>() {

            @Override
            public void onSuccess(EventTypeResponseBean result) {
                eventTypeList = result.getData();
                refreshView();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(EventTypeResponseBean result) {
                getBaseActivity().tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                getBaseActivity().tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                getBaseActivity().cancelLoadingDialog();
                getBaseActivity().cancelTitleLoading();
            }

            @Override
            public void onSuccessCache(EventTypeResponseBean result) {

            }
        });

    }

    public class EventTypeResponseBean extends BaseResponseBean implements Serializable {
        private List<OneEventTypeBean> data;

        public List<OneEventTypeBean> getData() {
            return data;
        }

        public void setData(List<OneEventTypeBean> data) {
            this.data = data;
        }
    }

    /**
     * 活动类型点击
     */
    AdapterView.OnItemClickListener eventTypeListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            OneEventTypeBean bean = eventTypeList.get(position);
            Intent intent = new Intent(getBaseActivity(), EventListActivity.class);
            intent.putExtra(IntentKey.EVENT_TYPE, bean);
            startActivity(intent);
        }
    };
}
